package com.CMUBiS.Depreciated;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.CMUBiS.Data.GlobalVars;

@WebServlet(name="LoginServletold2", urlPatterns="/loginold2")
public class LoginServlet extends HttpServlet{
	private static final long serialVersionUID = 6475041003023438743L;
	private static final String EMPTY = "";
	
	/**
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		StringBuffer errorString = new StringBuffer();
		Map<String, String[]> paramMap = request.getParameterMap();
		
		if(!paramMap.isEmpty()){
			// Check the username is present
			if(!paramMap.containsKey("username") || request.getParameter("username").equals(EMPTY)){
					errorString.append("You must provide a username. <br />");
			}

			// Check password is present
			if(!paramMap.containsKey("password") || request.getParameter("password").equals(EMPTY)){
				errorString.append("You must provide a password. <br />");
			}
			
			// If there are no errors we can try to login the user
			if(errorString.toString().equals("")){
				// Check the username and password
				int userID = GlobalVars.db.checkUsernamePassword(request.getParameter("username"), request.getParameter("password"));
				
				if(userID > 0){
					// Set the auth token for this logon session
					String authToken = GlobalVars.db.makeNewAuthToken(userID);
					
					if(authToken != null){
						// forward to user profile
						if(!paramMap.containsKey("welcome"))
							response.sendRedirect("./myaccount?userID=" + userID + "&authToken=" + authToken + "&welcome=" + request.getParameter("welcome"));
						else
							response.sendRedirect("./myaccount?userID=" + userID + "&authToken=" + authToken);
					}
					else{
						errorString.append("We appear to have a problem authorizing you. <br />");
					}
				}
				else{
					errorString.append("Sorry, your username or password was incorrect. <br />");
				}
			}
		}
		
		// If we get to here then there must be an error
		this.makePage(request, response, errorString.toString());
	}
	
	/**
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		makePage(request, response, "");
	}
	
	/**
	 * 
	 * @param request
	 * @param response
	 * @param errorString
	 * @throws ServletException
	 * @throws IOException
	 */
	public void makePage(HttpServletRequest request, HttpServletResponse response, String errorString)  throws ServletException, IOException {
		// We are using PrintWriter to be friendly to the international community. PrintWrite auto-converts coding
		PrintWriter out = response.getWriter();
		
		// Include header
		out.println("<HTML>");
		out.println("<HEAD><TITLE>Login</TITLE></HEAD>");
		out.println("<BODY>");
		out.println(errorString);
		
		// Do HTML4 or 5 depending on the global variables
		if(GlobalVars.isHTML4)
			createHTML4(request, response, out);
		else
			createHTML5(request, response, out);
	}
	
	/**
	 * 
	 * @param request
	 * @param response
	 * @param out
	 * @return
	 */
	public HttpServletResponse createHTML4(HttpServletRequest request, HttpServletResponse response, PrintWriter out){
		try{
			// Set the return type
			response.setContentType("text/html");
			
			// Get the URL parameters if any were passed
			Map<String, String[]> paramMap = request.getParameterMap();
			
			out.println("<form name=\"login\" action=\"login\" method=\"POST\">");
			out.println("<div id=\"center\">");	
			
			// Make the username box
			out.println("<div id=\"floatright\">Username: </div>");
			out.println("<div id=\"floatcenter\"> <input type=\"text\" name=\"username\" size=\"30\" value=\""+ (paramMap.containsKey("username") ?  request.getParameter("username") : "") + "\"/> </div>");
			
			// Make the password box
			out.println("<div id=\"floatright\">Password: </div>");
			out.println("<div id=\"floatcenter\"> <input type=\"text\" name=\"password\" size=\"30\" value=\""+ (paramMap.containsKey("password") ?  request.getParameter("password") : "") + "\"/> </div>");
			
			out.println("<div id=\"floatright\"><input type=\"submit\" value=\"Login\"></div>");
			
			out.println("</div>");
			out.println("</form>");
			
			out.println(request.getParameter("userID"));
			
			// Include footer
			out.println("</BODY></HTML>");
		}
		catch(Exception e){
			e.printStackTrace();
		}
		
		return response;
	}
	
	public HttpServletResponse createHTML5(HttpServletRequest request, HttpServletResponse response, PrintWriter out){
		try{
			
			
		}
		catch(Exception e){
			e.printStackTrace();
		}
		
		return response;
	}
}
